package com.kedacom.emptyproject.adapter

import android.databinding.ViewDataBinding
import android.graphics.Color
import android.support.v4.content.ContextCompat
import com.kedacom.emptyproject.MainApplication
import com.kedacom.emptyproject.R
import com.kedacom.emptyproject.databinding.ItemScheduleDayBinding
import com.kedacom.emptyproject.model.bean.ScheduleItemInfo
import com.kedacom.emptyproject.model.bean.TaskColor
import com.kedacom.lego.adapter.recyclerview.LegoBaseRecyclerViewAdapter

/**
 * <pre>
 *     根据服务端的返回值，不同的状态，显示不同的背景色。
 *     作者：Caowj
 *     邮箱：caoweijian@kedacom.com
 *     日期：2022/4/26 18:07
 * </pre>
 */
class ScheduleRecyclerAdapter(
    layoutId: Int,
    data: List<ScheduleItemInfo>?,
    brVariableId: Int
) :
    LegoBaseRecyclerViewAdapter<ScheduleItemInfo>(layoutId, data, brVariableId) {

    override fun onCustomBindItem(binding: ViewDataBinding, position: Int) {
        super.onCustomBindItem(binding, position)
        val scheduleDayBinding: ItemScheduleDayBinding = binding as ItemScheduleDayBinding
        val scheduleItemInfo = data[position]

        // 不同的状态，显示不同的颜色
        when {
            scheduleItemInfo?.taskCount == 0 -> {
                scheduleDayBinding.tvItemScheduleDay.setTextColor(
                    MainApplication.instance.getColor(R.color.gray_33)
                )
                scheduleDayBinding.tvItemScheduleDay.background = null
            }
            scheduleItemInfo?.taskState == TaskColor.BLUE -> {
                val finishedDrawable = ContextCompat.getDrawable(
                    MainApplication.instance,
                    R.drawable.bg_item_schedule_day_blue
                )
                scheduleDayBinding.tvItemScheduleDay.background = finishedDrawable
                scheduleDayBinding.tvItemScheduleDay.setTextColor(Color.WHITE)
            }
            scheduleItemInfo?.taskState == TaskColor.RED -> {
                val unFinishedDrawable = ContextCompat.getDrawable(
                    MainApplication.instance,
                    R.drawable.bg_item_schedule_day_red
                )
                scheduleDayBinding.tvItemScheduleDay.background = unFinishedDrawable
                scheduleDayBinding.tvItemScheduleDay.setTextColor(Color.WHITE)
            }
            scheduleItemInfo?.taskState == TaskColor.YELLOW -> {
                val unFinishedDrawable = ContextCompat.getDrawable(
                    MainApplication.instance,
                    R.drawable.bg_item_schedule_day_yellow
                )
                scheduleDayBinding.tvItemScheduleDay.background = unFinishedDrawable
                scheduleDayBinding.tvItemScheduleDay.setTextColor(Color.WHITE)
            }
            else -> {
                scheduleDayBinding.tvItemScheduleDay.setTextColor(
                    MainApplication.instance.getColor(R.color.white)
                )
                scheduleDayBinding.tvItemScheduleDay.background = null
            }
        }
        // 其他月份的日期文字置灰
        if (scheduleItemInfo?.isCurrentMonthDay == false) {
            scheduleDayBinding.tvItemScheduleDay.setTextColor(
                MainApplication.instance.getColor(R.color.gray_aa)
            )
            scheduleDayBinding.tvItemScheduleDay.background = null
        }
    }
}